import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import api, { type userinfoModel } from '@/api'
import { getItem, setItem } from '@/utils/token-utils'
import { Toast } from 'tdesign-mobile-vue'


const initUserinfo = (): userinfoModel => ({
  cellular: '',
  uuid: '',
  token: '',
  regOrLogChoice: ''

})

interface UnconfirmResultItemModel {
  unconfirmAmt: string,
  applyItemOfHome: {
    applyIdOfHome: string,
    applyLogoOfHome: string,
    applyNameOfHome: string,
  }
}

export const useOverAllStore = defineStore('overAll', {
  state: () => ({
    token: getItem('token') as string,
    
    userinfo: initUserinfo(),
    
    homeScreensData: {
      scienFlag: '',
      showAmt: '',
      firstOrAgain: '',
      refuseTime: '',
      repayModule: {
        repayCount: '',
        repayAmt: '',
        applyIdOfHome: '',
        OrderIdOfHome: ''
      },
      unconfirmResult: {
        unconfirmAmt: '',
        applyItemOfHome: {
          applyIdOfHome: '',
          applyLogoOfHome: '',
          applyNameOfHome: '',
        }
      },
      amountResOfConfirm: '',
    }
  }),

  actions: {

   
    async getHomeScreenStoreHandler(data: any) {
      try {
        let result = (await api.getHomeScreen(data)).data
        
        if (result.code == '0') {
          this.homeScreensData = result.data
        } else {
          Toast({
            message: result.message
          })
        }

        return result
      } catch (error) {

        return Promise.reject(error)
      }
    },


    async login(data: any) {
      try {
        let result = (await api.login(data)).data
        if (result.code == '0') {
          this.token = result.data.token
   
          setItem('token', result.data.token)
          setItem('cellular', result.data.cellular)
          setItem('uuid', result.data.uuid)
          this.userinfo = result.data

        }

        return result
      } catch (error) {

        return Promise.reject(error)
      }
    },



  }

})
